package com.laundry.controller;

import com.laundry.dto.ResultDTO;
import com.laundry.entity.ClothesType;
import com.laundry.service.ClothesTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/clothes")
public class ClothesTypeController {
    
    @Autowired
    private ClothesTypeService clothesTypeService;
    
    @GetMapping("/list")
    public ResultDTO getClothesTypes() {
        try {
            return ResultDTO.success(clothesTypeService.findAll());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.error("获取衣物类型列表失败");
        }
    }
    
    @GetMapping("/detail")
    public ResultDTO getClothesTypeById(@RequestParam Long id) {
        try {
            return ResultDTO.success(clothesTypeService.findById(id));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.error("获取衣物类型详情失败");
        }
    }
    
    @PostMapping("/create")
    public ResultDTO createClothesType(@RequestBody ClothesType clothesType) {
        try {
            return ResultDTO.success(clothesTypeService.save(clothesType));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.error("创建衣物类型失败");
        }
    }
    
    @PutMapping("/update")
    public ResultDTO updateClothesType(@RequestBody ClothesType clothesType) {
        try {
            return ResultDTO.success(clothesTypeService.update(clothesType));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.error("更新衣物类型失败");
        }
    }
    
    @DeleteMapping("/delete")
    public ResultDTO deleteClothesType(@RequestParam Long id) {
        try {
            clothesTypeService.deleteById(id);
            return ResultDTO.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.error("删除衣物类型失败");
        }
    }
} 